//
//  DetailViewController.swift
//  第二天项目演练
//
//  Created by mac on 16/5/9.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class DetailViewController: UIViewController {
    
    var closure :(() -> ())?
    
    @IBOutlet weak var nameField: UITextField!
    
    @IBOutlet weak var ageField: UITextField!
    
    var person :Person?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        nameField.text = person?.name
        ageField.text = "\(person?.age ?? 0)"
        
    }

    @IBAction func textChange(sender: AnyObject) {
        
        if nameField.hasText() && ageField.hasText() {
        
            navigationItem.rightBarButtonItem?.enabled = true
            
        }else {
        
            navigationItem.rightBarButtonItem?.enabled = false
        }
    }
    
    @IBAction func saveButtonClick(sender: AnyObject) {
        
        person?.name = nameField.text
        let age = ageField.text
        person?.age = Int(age!) ?? 0
        
        closure?()
        
        navigationController?.popViewControllerAnimated(true)
    }
    

}
